/*
 * @Author: xiaosihan 
 * @Date: 2024-05-27 13:49:37 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-05-28 01:06:22
 */
import { ProvincesData } from "@views/home/type";
import { AdditiveBlending, CircleGeometry, Clock, DoubleSide, Group, Mesh, MeshBasicMaterial, PlaneGeometry, Vector3 } from "three";
import threeLoader from "three-base/threeLoader";
import provincesDataJSON from "../provincesData.json";
import flyLineFocusMapPNG from "./flyLineFocusMap.png";
import { concat } from "lodash";

//飞线焦点分组
export default class FlyLineFocusGroup extends Group {
    constructor() {
        super();
        this.setData(provincesDataJSON);
    }

    //地图偏移量
    offset = new Vector3(-116.468994140625 * 5, 0, 40.25177764892578 * 5);

    //飞线的焦点模型
    flyLineFocusGeometry = new CircleGeometry(0.2, 100);

    // 飞线贴图
    flyLineFocusTexture = (() => {
        const texture = threeLoader.getTexture(flyLineFocusMapPNG);
        texture.center.set(0.5, 0.5);
        return texture;
    })();

    flyLineFocusMaterial = new MeshBasicMaterial({
        color: 0xffffff,
        map: this.flyLineFocusTexture,
        alphaMap: this.flyLineFocusTexture,
        transparent: true,
        fog: false,
        side: DoubleSide,
        depthTest: false,
        blending: AdditiveBlending,
    })

    // 设置飞线焦点数据
    setData(provincesDatas: Array<ProvincesData>) {
        this.clear();

        // 创建飞线焦点
        provincesDatas
            .filter((item, index) => item.value > 10)
            .concat([
                {
                    "name": "东城区",
                    "enName": "东城区",
                    "center": [
                        116.418757,
                        39.917544
                    ],
                    "centroid": [
                        116.416739,
                        39.912912
                    ],
                    "value": 9
                }
            ])
            .map((city) => {
                let [x, y] = city.centroid;
                let point = new Vector3(x, 0.1, -y).multiplyScalar(5).add(this.offset);
                const mesh = new Mesh(this.flyLineFocusGeometry, this.flyLineFocusMaterial);
                mesh.position.copy(point);
                mesh.rotation.x = Math.PI / 2;
                mesh.renderOrder = 21
                this.add(mesh)
            });

    }

    clock = new Clock();

    updateMatrix(): void {
        super.updateMatrix();

        // 贴图旋转动画
        this.flyLineFocusTexture.rotation -= (this.clock.getDelta() * 6);
    }
}